package com.forensix.interface_test.product_manage;

import com.forensix.interface_test.base.BaseSystem;
import com.forensix.interface_test.data.ProductData;
import com.forensix.interface_test.pojo.Product;
import com.forensix.interface_test.utils.FakerDataUtil;
import com.forensix.interface_test.utils.GetBaseDirUtil;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import org.testng.annotations.Test;
import java.io.File;

import java.util.HashMap;
import java.util.Map;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;


public class ProductApiImpl implements ProductApi{

    String url = BaseSystem.getUrl();
    String cookie = BaseSystem.getCookie();
    String cookieKey = BaseSystem.cookieKey;
    Product product = new Product();

    private void extracted() {
        product.setProductName("productName");
        product.setProductModules("productModules[]");
        product.setClientId("clientId");
    }

    @Test
    @Override
    public void newAddProduct() {

        Map<String, Object> map = new HashMap<>();

        extracted();
        String productName = product.getProductName();
        String productModules = product.getProductModules();
        String clientId = product.getClientId();
        map.put(productName, FakerDataUtil.getName());
        map.put(clientId, FakerDataUtil.getPassword());
        map.put(productModules, FakerDataUtil.getName());


        given().
                cookie(cookieKey, cookie)
                .when()
                .log().all()
                .formParams(map)
                .multiPart("logo",new File(GetBaseDirUtil.getBaseDir() + "\\data\\image\\1.jpg"))
                .post(url + "/product")
                .then()
                .log().all()
                .body("message", equalTo("创建成功")); // 断言 需要引入静态方法：import static org.hamcrest.Matchers.*;

    }

    @Test
    @Override
    public void GetProductList() {
        getValidatableResponse();
    }


    @Test
    public Object GetProductListId() {

        Response response = getValidatableResponse()
//                .body("count", equalTo(32))
                .extract().response();

        Object o = response.path("data[0]._id");
        System.out.println(o);
        return o;

    }

    private ValidatableResponse getValidatableResponse() {
        return given().cookie(cookieKey, cookie)
                .when()
                .log().all()
                .get(url + "/product")
                .then()
                .log().all()
                .statusCode(200);
    }

    @Test
    @Override
    public void GetProductDetail() {
        given().cookie(cookieKey, cookie)
                .when()
                .log().all()
                .get(url + "/product/" + GetProductListId())
                .then()
                .log().all();
    }

    @Test(dataProvider = "ProductParams", dataProviderClass = ProductData.class)
    private void testDataParams(String productName, String clientId, String productModules){

        Map<String, Object> map = new HashMap<>();

        System.out.println(productName+"    "+productModules + "   " + clientId);
        extracted();
        map.put(product.getProductName(), productName);
        map.put(product.getClientId(), clientId);
        map.put(product.getProductModules(), productModules);

        given().
                cookie(cookieKey, cookie)
                .when()
                .log().all()
                .formParams(map)
                .multiPart("logo",new File(GetBaseDirUtil.getBaseDir() + "\\data\\image\\1.jpg"))
                .post(url + "/product")
                .then()
                .log().all()
                .body("message", equalTo("创建成功"));
    }


}
